package org.study4j.message.mq.rabbitmq.util;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.study4j.message.mq.rabbitmq.dto.BaseRabbitDTO;

import java.util.UUID;

@Component
public class RabbitMQBaseSend {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * @param rabbitDto rabbit消息实体
     * @throws Exception
     * @description rabbit消息发送
     * @author jiangbin
     * @date 2019年1月20日 下午9:39:14
     * @version 1.0.0
     */
    public void sendMsg(BaseRabbitDTO rabbitDto) throws Exception {
        try {
            CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
            rabbitTemplate.convertAndSend(rabbitDto.getExchange(), rabbitDto.getRoutingKey(), rabbitDto.getMsg(), correlationData);
        } catch (Throwable e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

}
